package com.example.officialdocumentsys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.officialdocumentsys.annotation.LogAnno;
import com.example.officialdocumentsys.common.R;
import com.example.officialdocumentsys.common.ThreadLocalContext;
import com.example.officialdocumentsys.po.FileCategoryList;
import com.example.officialdocumentsys.po.User;
import com.example.officialdocumentsys.service.FilecategorylistService;
import com.example.officialdocumentsys.vo.FileCategoryListVO;
import com.example.officialdocumentsys.vo.UserVO;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hi
 * @since 2022-11-24
 */
@RestController
@RequestMapping("/file-category-list")
public class FileCategoryListController extends BaseController<FileCategoryList, FilecategorylistService>{
    //归档
    @PostMapping("/sort")
    public R<String> sort(@RequestBody  Map map){
        Long sortId = Long.valueOf(map.get("sortId").toString());
        Long fileId = Long.valueOf(map.get("fileId").toString());
        //构造条件
        FileCategoryList filecategorylist = new FileCategoryList();
        filecategorylist.setFileId(fileId);
        filecategorylist.setCategoryId(sortId);
        Long currentId = ThreadLocalContext.getCurrentId();
        filecategorylist.setUserId(currentId);
        QueryWrapper<FileCategoryList> qw = new QueryWrapper<>();
        qw.lambda().eq(FileCategoryList::getFileId,fileId).eq(FileCategoryList::getUserId,currentId);
        FileCategoryList one = baseService.getOne(qw);
        if (one != null){
            filecategorylist.setId(one.getId());

        }
        baseService.saveOrUpdate(filecategorylist);
        return R.success("归档成功");

    }

    //分页
    @LogAnno(operateType = "浏览归档")
    @GetMapping("/pagePlus")
    public R<Page> pagePlus(int page, int pageSize,Long sortId) {
        QueryWrapper<FileCategoryListVO> qw = new QueryWrapper<>();
        qw.eq(sortId != null,"q.category_id",sortId);
        qw.lambda().eq(FileCategoryListVO::getUserId,ThreadLocalContext.getCurrentId());
        Page<FileCategoryListVO> pageInfo = new Page(page, pageSize);
        return R.success(baseService.getVO(pageInfo,qw));
    }

}

